package com.robooot.business_counter.controller;

import com.alibaba.fastjson.JSON;
import com.robooot.business_counter.entity.Courier;
import com.robooot.business_counter.entity.Device;
import com.robooot.business_counter.entity.Store;
import com.robooot.business_counter.entity.UserInfo;
import com.robooot.business_counter.model.*;
import com.robooot.business_counter.service.CourierService;
import com.robooot.business_counter.service.StoreService;
import com.robooot.business_counter.service.UserInfoService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.invoke.LambdaConversionException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business-counter")
public class StoreController extends BaseController{

    @Autowired
    private StoreService storeService;
    @Autowired
    private CourierService courierService;
    @Autowired
    private UserInfoService userInfoService;

    private final Logger logger = LoggerFactory.getLogger(getClass());



    /**
     * 获取所有存放的快递信息
     * @return
     */
    @PostMapping("/getPickupRecordList")
    public ResultModel getPickupRecordList(@RequestBody StoreVo storeVo){
        pageHelper(storeVo);
        Integer count = storeService.getPickupRecordCount(storeVo);
        List<Store> list = storeService.getPickupRecordList(storeVo);
        return successPage(storeVo,list,count);
    }


    /**
     * 添加邮递员
     * @return
     */
    @PostMapping("/addCourier")
    public ResultModel addCourier(@RequestBody CourierVo courierVo){
        courierService.addCourier(courierVo);
        return ok();
    }

    /**
     * 修改邮递员
     * @return
     */
    @PostMapping("/updateCourier")
    public ResultModel updateCourier(@RequestBody CourierVo courierVo){
        courierService.updateCourier(courierVo);
        return ok();
    }

    /**
     * 删除邮递员
     * @return
     */
    @GetMapping("/deleteCourier")
    public ResultModel deleteCourier(@RequestParam String courierId){
        courierService.deleteCourier(courierId);
        return ok();
    }

    /**
     * 查看快递员权限
     */
    @GetMapping("/getPermission")
    public ResultModel getPermission(@RequestParam String phone){
        logger.info("查看快递员权限============"+phone);
        Courier courier = courierService.getCourier(phone);
        if(courier != null){
            return ok(courier);
        }
        return ok();
    }

    /**
     * 查看所有邮递员列表
     * @return
     */
    @PostMapping("/getCourierList")
    public ResultModel getCourierList(@RequestBody CourierVo courierVo)  {
        logger.info(JSON.toJSONString(courierVo) + "入参=========================");
        ResultModel model = new ResultModel();
        model.setMessage("您没有权限查看哦");
        model.setResult(-1);
        pageHelper(courierVo);
        if(StringUtils.isBlank(courierVo.getPhone())) return model;
        Courier courier = courierService.getCourier(courierVo.getPhone());
        logger.info(JSON.toJSONString(courier) + "快递员信息=========================");
        if ("1".equals(courier.getPermission())){
            List<Courier> list = courierService.getCourierList(courierVo);
            Integer count = courierService.getCourierCount(courierVo);
            return successPage(courierVo,list,count);
        }else {
            return model;
        }

    }


    /**
     * 再次向用户发送短信
     * @return
     */
    @PostMapping("/sendMessages")
    public ResultModel sendMessages(@RequestBody CheckVo checkVo){
        JSONObject device = storeService.getDevice(checkVo.getDeviceId());
        JSONObject location = storeService.getDeviceLocation(checkVo.getDeviceId());
        String address = location.getString("mitName")+location.getString("shiName")+location.getString("quName")+"-"+location.getString("location");
        courierService.sendMessages(checkVo.getUserPhone(), checkVo.getCounterNumber(),device.getString("device_name"),checkVo.getCourierPhone(),address);
        return ok();
    }


    /**
     * 通过token获取用户信息并保存
     * @return
     */
    @GetMapping("/getUserInfo")
    public ResultModel getUserId(@RequestParam String token){
        logger.info("通过token获取用户信息并保存============"+token);
        String userId = userInfoService.getUserId(token);
        UserInfo userInfo = userInfoService.getUserInfo(userId);
        return ok(userInfo);
    }


    /**
     * 获取单个快递柜门柜状态
     * @param deviceVo
     * @return
     */
    @PostMapping("/getDeviceDoor")
    public ResultModel getDeviceDoor(@RequestBody DeviceVo deviceVo){
        pageHelper(deviceVo);
        List<Device> deviceDoor = storeService.getDeviceDoor(deviceVo);
        deviceVo.setStatus("Z");
        Integer count = storeService.getDeviceDoorCount(deviceVo);
        if(count == 0){
            deviceVo.setStatus(null);
             count = storeService.getDeviceDoorCount(deviceVo);
        }
        return successPage(deviceVo,deviceDoor,count);
    }


    /**
     * 获取所有快递柜列表
     * @return
     */
    @GetMapping("/getDeviceList")
    public ResultModel getDeviceList(){
        logger.info("获取所有快递柜列表============");
        logger.info(JSON.toJSONString(ok(storeService.getDeviceList())));
        return ok(storeService.getDeviceList());
    }


    /**
     * 快递柜开柜门和状态同步
     * @return
     */
    @PostMapping("/handle")
    public ApiResult handle(@RequestBody DevicePackage pg){
        Map handle = storeService.handle(pg);
        return ApiResult.ok(handle);
    }


    /**
     * 快递员存包(向用户发短信)
     */
    @PostMapping("/bagCheck")
    public ResultModel bagCheck(@RequestBody CheckVo checkVo){
        logger.info("入参====="+JSON.toJSONString(checkVo));
        UserVo userVo = userInfoService.getUser(checkVo.getUserPhone());
        if(userVo != null){
            userVo.setDeviceId(checkVo.getDeviceId());
            userVo.setCounterNumber(checkVo.getCounterNumber());
            //给用户推送闪易app消息
            userInfoService.forwardNews(userVo);
        }
        JSONObject device = storeService.getDevice(checkVo.getDeviceId());
        JSONObject location = storeService.getDeviceLocation(checkVo.getDeviceId());
        String address = location.getString("mitName")+location.getString("shiName")+location.getString("quName")+"-"+location.getString("location");
        courierService.sendMessages(checkVo.getUserPhone(),checkVo.getCounterNumber(),device.getString("device_name"),checkVo.getCourierPhone(),address);
        storeService.bagCheck(checkVo);
       return ok();
    }




}
